package org.apache.james.mime4j.io;

import java.io.IOException;
import java.io.InputStream;
import org.apache.james.mime4j.util.ByteArrayBuffer;

public class LineReaderInputStreamAdaptor extends LineReaderInputStream
{
  private final LineReaderInputStream bis;
  private boolean eof;
  private final int maxLineLen;
  private boolean used;

  public LineReaderInputStreamAdaptor(InputStream paramInputStream)
  {
    this(paramInputStream, -1);
  }

  public LineReaderInputStreamAdaptor(InputStream paramInputStream, int paramInt)
  {
    super(paramInputStream);
    this.used = false;
    this.eof = false;
    if (paramInputStream instanceof LineReaderInputStream)
      this.bis = ((LineReaderInputStream)paramInputStream);
    while (true)
    {
      this.maxLineLen = paramInt;
      return;
      this.bis = null;
    }
  }

  private int doReadLine(ByteArrayBuffer paramByteArrayBuffer)
    throws IOException
  {
    int j;
    int k;
    int i = 0;
    do
    {
      j = this.in.read();
      if (j == -1)
        break;
      paramByteArrayBuffer.append(j);
      ++i;
      if ((this.maxLineLen > 0) && (paramByteArrayBuffer.length() >= this.maxLineLen))
        throw new MaxLineLimitException("Maximum line length limit exceeded");
    }
    while (j != 10);
    if ((i == 0) && (j == -1))
      k = -1;
    while (true)
    {
      return k;
      k = i;
    }
  }

  public boolean eof()
  {
    return this.eof;
  }

  public boolean isUsed()
  {
    return this.used;
  }

  public int read()
    throws IOException
  {
    int j;
    int i = this.in.read();
    if (i == -1)
      j = 1;
    while (true)
    {
      this.eof = j;
      this.used = true;
      return i;
      j = 0;
    }
  }

  public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    int j;
    int i = this.in.read(paramArrayOfByte, paramInt1, paramInt2);
    if (i == -1)
      j = 1;
    while (true)
    {
      this.eof = j;
      this.used = true;
      return i;
      j = 0;
    }
  }

  public int readLine(ByteArrayBuffer paramByteArrayBuffer)
    throws IOException
  {
    int i;
    int j;
    if (this.bis != null)
    {
      i = this.bis.readLine(paramByteArrayBuffer);
      if (i != -1)
        break label45;
      j = 1;
    }
    while (true)
    {
      while (true)
      {
        this.eof = j;
        this.used = true;
        return i;
        i = doReadLine(paramByteArrayBuffer);
      }
      label45: j = 0;
    }
  }

  public String toString()
  {
    return "[LineReaderInputStreamAdaptor: " + this.bis + "]";
  }
}